###Clear R------------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("psych", "ggplot2", "interplot", "MASS", "foreign", "lubridate", "car", "stringr", "lavaan", "stringr")
ipak(packages)

### load data----------
load("Study3_replication.Rdata")


### Partisan Social Identity Strength------------
data$strength_dem_1_rec<-car::recode(data$strength_dem_1, "36=1;37=2; 39=3;40=4")
data$strength_dem_2_rec<-car::recode(data$strength_dem_2, "36=1;37=2; 39=3;40=4")
data$strength_dem_3_rec<-car::recode(data$strength_dem_3, "36=1;37=2; 39=3;40=4")
data$strength_dem_4_rec<-car::recode(data$strength_dem_4, "36=1;37=2; 39=3;40=4")
data$strength_dem_5_rec<-car::recode(data$strength_dem_5, "36=1;37=2; 39=3;40=4")
data$strength_dem_6_rec<-car::recode(data$strength_dem_6, "36=1;37=2; 39=3;40=4")
data$strength_dem_7_rec<-car::recode(data$strength_dem_7, "36=1;37=2; 39=3;40=4")
data$strength_dem_8_rec<-car::recode(data$strength_dem_8, "36=1;37=2; 39=3;40=4")

data$strength_rep_1_rec<-car::recode(data$strength_rep_1, "10=1;11=2; 13=3;14=4")
data$strength_rep_2_rec<-car::recode(data$strength_rep_2, "10=1;11=2; 13=3;14=4")
data$strength_rep_3_rec<-car::recode(data$strength_rep_3, "10=1;11=2; 13=3;14=4")
data$strength_rep_4_rec<-car::recode(data$strength_rep_4, "10=1;11=2; 13=3;14=4")
data$strength_rep_5_rec<-car::recode(data$strength_rep_5, "10=1;11=2; 13=3;14=4")
data$strength_rep_6_rec<-car::recode(data$strength_rep_6, "10=1;11=2; 13=3;14=4")
data$strength_rep_7_rec<-car::recode(data$strength_rep_7, "10=1;11=2; 13=3;14=4")
data$strength_rep_8_rec<-car::recode(data$strength_rep_8, "10=1;11=2; 13=3;14=4")

data$huddy1 <- ifelse(!is.na(data$strength_dem_1_rec),data$strength_dem_1_rec,data$strength_rep_1_rec)
data$huddy2 <- ifelse(!is.na(data$strength_dem_2_rec),data$strength_dem_2_rec,data$strength_rep_2_rec)
data$huddy3 <- ifelse(!is.na(data$strength_dem_3_rec),data$strength_dem_3_rec,data$strength_rep_3_rec)
data$huddy4 <- ifelse(!is.na(data$strength_dem_4_rec),data$strength_dem_4_rec,data$strength_rep_4_rec)
data$huddy5 <- ifelse(!is.na(data$strength_dem_5_rec),data$strength_dem_5_rec,data$strength_rep_5_rec)
data$huddy6 <- ifelse(!is.na(data$strength_dem_6_rec),data$strength_dem_6_rec,data$strength_rep_6_rec)
data$huddy7 <- ifelse(!is.na(data$strength_dem_7_rec),data$strength_dem_7_rec,data$strength_rep_7_rec)
data$huddy8 <- ifelse(!is.na(data$strength_dem_8_rec),data$strength_dem_8_rec,data$strength_rep_8_rec)

data$PSIDstrength<-1-rowMeans(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),na.rm=T) #note that here scale gets recoded to range from weak (0) to strong (1) pid strength
### Cognitive Reflection Test------------------

#CRT1: 5 cents
data$CRT1 <- tolower(data$CRT1)
data$CRT1 <- ifelse(data$CRT1==5,1,0)

#CRT2: 5minutes
data$CRT2 <- tolower(data$CRT2)
data$CRT2 <- ifelse(data$CRT2==5,1,0)

#CRT3: 47 days
data$CRT3 <- tolower(data$CRT3)
data$CRT3 <- ifelse(data$CRT3==47,1,0)

#CRT4: 4 days
data$CRT4 <- tolower(data$CRT4)
data$CRT4[str_detect(data$CRT4,"4 days")]=4
data$CRT4 <- ifelse(data$CRT4==4 | data$CRT4=="4?",1,0)

#CRT5: 29
data$CRT5 <- tolower(data$CRT5)
data$CRT5 <- ifelse(data$CRT5==29,1,0)

#CRT6: 20
data$CRT6 <- tolower(data$CRT6)
data$CRT6 <- ifelse(data$CRT6==20,1,0)

#CRT7: answer 3
data$CRT7 <- ifelse(data$CRT7==3,1,0)

#correlation matrix
cor(with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7)),use="pairwise.complete.obs")

#make trait
data$CRTall <- rowSums(with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7)),na.rm=T)
data$CRTall<-zero1(data$CRTall)

#Honest answer on CRT?
data$CRT_honest<-ifelse(data$CRT_honest==2,0,1) #all answer honest

#CRT seen before? Yes/maybe (1) or not (0)
data$CRT_seen01<-ifelse(data$CRT_seen==3,0,1) #1460 have not seen CRT before

### Need for Cognition----------------
#recode reverse coded items
data$NFC_3_rec<-6-data$NFC_3
data$NFC_4_rec<-6-data$NFC_4
data$NFC_5_rec<-6-data$NFC_5
data$NFC_7_rec<-6-data$NFC_7
data$NFC_12_rec<-6-data$NFC_12

data$NFC<-rowMeans(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(NFC_1, NFC_2, NFC_3_rec, NFC_4_rec, NFC_5_rec, NFC_7_rec, NFC_10, NFC_11, NFC_12_rec, NFC_13))),na.rm=T)
mean(data$NFC,na.rm=T)
### Exclude respondents that have missing values on CRT & NfC-----------------
data <- subset(data,CRT1>=0 & CRT2>=0 & CRT3>=0 & CRT4>=0 & CRT5>=0 & CRT6>=0 & CRT7>=0 & NFC_1>=1 & NFC_2>=1 & NFC_3_rec>=1 & NFC_4_rec>=1 & NFC_5_rec>=1 & NFC_7_rec>=1 & NFC_10>=1 & NFC_11>=1 & NFC_12_rec>=1 & NFC_13>=1)

### Latent Cognitive Resources dimension ----------------
latent <- ' cogresources  =~ NA*CRT1+CRT2+CRT3+CRT4+CRT5+CRT6+CRT7+NFC_1+NFC_2+NFC_3_rec+NFC_4_rec+NFC_5_rec+NFC_7_rec+NFC_10+ NFC_11+ NFC_12_rec+ NFC_13
# fix variance of latent variable
cogresources ~~ 1*cogresources'

fit <- cfa(latent, ordered=c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7", "NFC_1", "NFC_2", "NFC_3_rec", "NFC_4_rec", "NFC_5_rec", "NFC_7_rec", "NFC_10", "NFC_11", "NFC_12_rec", "NFC_13"), data=data)
#fit model
data$cogresources<-(lavPredict(fit, type = "lv", method="EBM", label = TRUE, optim.method = "nlminb"))
data$cogresources<-zero1(data$cogresources)

### Covariates--------------------

#Female
data$female<-ifelse(data$gender=="Female",1,0)

#Education
#1. less than high school &  High school
#2. some college / Vocational training /  Associate degree
#3.  College / Professional / Master / Doctorate
data$edu<-car::recode(as.numeric(as.factor(data$education)), "4=1; 5=1; 8=2; 9=2; 1=2; 2=3; 3=3; 6=3; 7=3")
#Race
data$non_white<-ifelse(data$ethnicity.1=="White/Caucasian",0,1)

#Party
data$Republican_dummy<-0
data$Republican_dummy[data$pid1==1]=1
data$Republican_dummy[data$pid4==1]=1

#PID# 
data$partisanship <- NA
data$partisanship[which(data$pid4==3)]=4
data$partisanship[which(data$pid4==1)]=3
data$partisanship[which(data$pid4==2)]=5
data$partisanship[which(data$pid3==2)]=2
data$partisanship[which(data$pid3==1)]=1
data$partisanship[which(data$pid2==2)]=6
data$partisanship[which(data$pid2==1)]=7

#PID strength
data$pidstrength <- (zero1(abs(data$partisanship-4)))

### Food Irradiation: Treatment ---------
data$treatment<-NA
data$treatment[data$FoodIrradiation_DO_Food_control=="2"]=1 #control
data$treatment[data$FoodIrradiation_DO_Food_dem_sup=="2"]=2 #Democrat
data$treatment[data$FoodIrradiation_DO_Food_rep_sup=="2"]=3 #Republican

### Food Irradiation: Create Dependent variable--------------

#Item 1 (Kam: support for irradiation)
data$DV_support_rec<-(6-data$DV_support)
#Item 2: Costs outweigh benefits
#recode so that opposition is the highest value
data$DV_costs_rec<-(6-(data$DV_costs-5))
#Item 3: food irradiation is bad
#recode so that opposition is the highest value
data$DV_bad_rec<-(6-data$DV_bad)
#create scale: high score is ban irradiation
data$DV_irradiation<-rowMeans(data.frame(data$DV_support_rec, data$DV_costs_rec,data$DV_bad_rec), na.rm=T)
data$DV_irradiation<- zero1(data$DV_irradiation)

#Food Irradiation: answers on quiz
data$Quiz1_cor<-ifelse(data$Food_quiz1==3,1,0)
data$Quiz2_cor<-ifelse(data$Food_quiz2==2,1,0)
data$Quiz3_cor<-ifelse(data$Food_quiz3==1,1,0)
data$Quiz4_cor<-ifelse(data$Food_quiz4==3,1,0)
data$Quiz_correct<-(data$Quiz1_cor+data$Quiz2_cor+data$Quiz3_cor+data$Quiz4_cor)

##Food Irradiation: Thoughts while reading
#number of characters
j<-as.vector(data$Food_thoughts)
data$nchar1 <- c(nchar(j))

#number of words
data$words<-sapply(strsplit(j, " "), length)
summary(data$words) #summar of #n of words

### Food Irradiation: In and Out-party Cues--------------

#In-party
# In-party endorsement: 0 (out-party endorses ban or no party cues); 1 (leaning, weak or strong partisan, in-party endorses ban). 
#Inparty endorsement
data$InParty_food<-NA
data$InParty_food[(data$treatment==3 & data$pid1==2) | (data$treatment==3 & data$pid4==2) | (data$treatment==2 & data$pid1==1 )| (data$treatment==2 & data$pid4==1)|data$treatment==1]=0
data$InParty_food[(data$treatment==2 & data$pid1==2 )| (data$treatment==2 & data$pid4==2 )]=1
data$InParty_food[(data$treatment==3 & data$pid1==1 )| (data$treatment==3 & data$pid4==1) ]=1
table(data$InParty_food)
#Out-party
#Out-party endorsement: 0 (in-party endorses ban or no party cues); 1 (leaning partisan; weak or strong partisan, out-party endorses ban).
#outparty
data$OutParty_food<-NA
data$OutParty_food[data$treatment==3 & data$pid1==1 | data$treatment==3 & data$pid4==1 | data$treatment==2 & data$pid1==2 | data$treatment==2 & data$pid4==2 | data$treatment==1]=0
data$OutParty_food[data$treatment==2 & data$pid1==1 | data$treatment==2 & data$pid4==1 ]=1
data$OutParty_food[data$treatment==3 & data$pid1==2 | data$treatment==3 & data$pid4==2 ]=1
table(data$OutParty_food)

#create party cue
data$PartyCue_food<-ifelse(data$InParty_food==1 | data$OutParty_food==1,1,0)

### Farm Policy: Treatment -------------------
data$treatment_farm<-NA
data$treatment_farm[data$Farmpolicy_DO_Farm_control=="2"]=1 #control
data$treatment_farm[data$Farmpolicy_DO_Farm_dem_sup=="2"]=2 #Democrat
data$treatment_farm[data$Farmpolicy_DO_Farm_rep_sup=="2"]=3 #Republican

### Farm Policy: Create DV--------------

#support
data$Farm_dv_support_rec<-(8-data$Farm_dv_support)
data$Farm_dv_support_rec<-zero1(data$Farm_dv_support_rec)

##Farm Policy: Score Quiz
data$Farm_quiz1_cor<-ifelse(data$Farm_quiz1==3,1,0)
data$Farm_quiz2_cor<-ifelse(data$Farm_quiz2==3,1,0)
data$Farm_Quiz_correct<-(data$Farm_quiz1_cor+data$Farm_quiz2_cor)


### Farm Policy: In and Out-party Cues--------------
#In-party
# In-party endorsement: 0 (out-party endorses ban or no party cues); 1 (leaning, weak or strong partisan, in-party endorses ban). 
#Inparty endorsement
data$InParty_farm<-NA
data$InParty_farm[(data$treatment_farm==3 & data$pid1==2) | (data$treatment_farm==3 & data$pid4==2) | (data$treatment_farm==2 & data$pid1==1 )| (data$treatment_farm==2 & data$pid4==1)|data$treatment_farm==1]=0
data$InParty_farm[(data$treatment_farm==2 & data$pid1==2 )| (data$treatment_farm==2 & data$pid4==2 )]=1
data$InParty_farm[(data$treatment_farm==3 & data$pid1==1 )| (data$treatment_farm==3 & data$pid4==1) ]=1
#Out-party
#Out-party endorsement: 0 (in-party endorses ban or no party cues); 1 (leaning partisan; weak or strong partisan, out-party endorses ban).
#outparty
data$OutParty_farm<-NA
data$OutParty_farm[data$treatment_farm==3 & data$pid1==1 | data$treatment_farm==3 & data$pid4==1 | data$treatment_farm==2 & data$pid1==2 | data$treatment_farm==2 & data$pid4==2 | data$treatment_farm==1]=0
data$OutParty_farm[data$treatment_farm==2 & data$pid1==1 | data$treatment_farm==2 & data$pid4==1 ]=1
data$OutParty_farm[data$treatment_farm==3 & data$pid1==2 | data$treatment_farm==3 & data$pid4==2 ]=1

#create party cue
data$PartyCue_farm<-ifelse(data$InParty_farm==1 | data$OutParty_farm==1,1,0)


#Save file---------
save(data, file="Study3_data.Rdata")

